<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-button type="primary">添加角色</el-button>
      <!-- 表格 -->
      <el-table :data="rolesList" stripe border>
        <!-- 展开行 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item1, index1) in scope.row.children"
              :key="item1.id"
              class="b center"
              :class="index1 == 0 ? 't' : ''"
            >
              <!-- 一级权限占的位置 -->
              <el-col :span="5">
                <el-tag closable @close="hendleClose(scope.row, item1.id)">{{
                  item1.authName
                }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级权限占的位置 -->
              <el-col :span="19">
                <el-row
                  v-for="(item2, index2) in item1.children"
                  :key="item2.id"
                  class="t center"
                  :class="index2 == 0 ? 'none' : ''"
                >
                  <!-- 二级权限占的位置 -->
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="hendleClose(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i
                  ></el-col>

                  <el-col :span="18">
                    <el-tag
                      closable
                      type="warning"
                      v-for="(item3, index3) in item2.children"
                      :key="item3.id"
                      @close="hendleClose(scope.row, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- <pre>
                  {{ scope.row }}
               </pre > -->
          </template>
        </el-table-column>
        <!-- 展开行 -->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="handleSettingRights(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限的展示框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="settingRightsDialogVisible"
      width="50%"
    >
     <!-- 分配权限树形结构 -->
      <el-tree :data="rightsList" :props="treeProps" show-checkbox node-key='id'
       default-expand-all :default-checked-keys='arrList' ref="treeRef"></el-tree>
      <span slot="footer">
        <el-button @click=" cancelFenPeiRight">取 消</el-button>
        <el-button type="primary" @click="suerFenPeiRight"
          >确 定</el-button
        >
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getRoles } from "../../api/roles";
import { deledeRightById } from "../../api/deledeRight";
import { getRights } from "../../api/getRughts";
import {roleAlloRight} from '../../api/roleAlloRight'
export default {
  data() {
    return {
      rolesList: [],
      settingRightsDialogVisible: false,
      rightsList: [],
      treeProps:{
          children: 'children',
          label: 'authName'
      },
      arrList:[],
      roleId:''
    };
  },
  created() {
    this.getRolesData();
  },
  methods: {
    async getRolesData() {
      const res = await getRoles();
      this.rolesList = res.data.data;
      // console.log(this.rolesList);
    },
    //  点击删除三级权限
    async hendleClose(row, id2) {
      //   console.log(id1,id2)
      const res = await deledeRightById(row.id, id2);
      //   console.log(res)
      this.$message.success("删除成功");
      row.children = res.data.data;
      //   this.getRolesData();
    },
    // 点击分配权限
    async handleSettingRights(role) {
        this.roleId =role.id
      // 获取权限列表
      const res = await getRights("tree");
      this.rightsList = res.data.data;
      // console.log(this.rightsList);
      this.arrList = []
       this.getChildrenId(role,this.arrList)
      //  console.log(this.arrList);
       this.settingRightsDialogVisible = true;
    },
    // 递归方法获取第三层的id
        getChildrenId(node,arr){
            if(!node.children){
              return arr.push(node.id)
            }
            node.children.forEach(item => {
               this.getChildrenId(item,arr)
            });
        },
        // 点击取消分配权限
        cancelFenPeiRight(){
          this.settingRightsDialogVisible = false
        },
        // 点击确定分配权限
       async suerFenPeiRight(){
          // this.settingRightsDialogVisible = false
          
          var arr1 = this.$refs.treeRef.getHalfCheckedNodes()
          var arr2 = this.$refs.treeRef.getCheckedKeys()
          var arr3 = []
          
          // var arr3 = [...arr1,...arr2]
          arr1.forEach(item=>{
            // console.log(item.id)
            arr3.push(item.id)
          })
          // console.log(arr3)
          arr3 = [...arr3,...arr2]
          // console.log(arr3)
          var str = arr3.join(',')
          // console.log(str)
          // console.log( this.roleId)

         const res = await roleAlloRight(this.roleId,str)
        //  console.log(res)
           this.getRolesData();
           this.settingRightsDialogVisible = false
        }
  },
};
</script>

<style>
</style>